package com.hmall.api.config;

import feign.RequestInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import feign.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;

public class DefaultFeginConfig {

    /**
     * 等级配置
     */

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return template->{
            //0、获取httpservletrequest
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
           HttpServletRequest request = requestAttributes.getRequest();
           //1.获取所有请求头的ID
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()){
                String key = headerNames.nextElement().toString();
                String value = request.getHeader(key);
                //一定移除
                if("content-length".equals(key)){
                    continue;
                }
                //2.全部再封装到请求头
                template.header(key,value);
            }
        };
    }
}
